package com.cloudcross.ssp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.adinall.dsp.core.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.model.RoleAccount;
import com.cloudcross.ssp.model.Roles;
import com.cloudcross.ssp.service.IRolesService;
import com.cloudcross.ssp.web.widget.SelectorController.ISelector;
import com.cloudcross.ssp.web.widget.SelectorController.ISelectorProvider;
import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.Common;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.pulgin.mybatis.plugin.PageView;

@Service
public class RolesService implements IRolesService, ISelectorProvider {
    @Autowired
    private GenericIBatisDao myBatisDao;

    @Override
    public PageView query(PageView pageView, Roles t) {
        // TODO Auto-generated method stub
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("paging", pageView);
        map.put("t", t);
        List<Roles> list = myBatisDao.getList("rolesSqlMapper.query", map);
        pageView.setRecords(list);
        return pageView;
    }

    @Override
    public List<Roles> queryAll(Roles t) {
        // TODO Auto-generated method stub
        return myBatisDao.getList("rolesSqlMapper.queryAll", t);
    }

    @Override
    public void delete(String id) throws Exception {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.delete", id);
    }

    @Override
    public void update(Roles t) throws Exception {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.update", t);
    }

    @Override
    public Roles getById(String id) {
        // TODO Auto-generated method stub
        return myBatisDao.get("rolesSqlMapper.getById", id);
    }

    @Override
    public void add(Roles t) throws Exception {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.add", t);
    }

    @Override
    public Roles isExist(String name) {
        // TODO Auto-generated method stub
        return myBatisDao.get("rolesSqlMapper.getByName", name);
    }

    @Override
    public Roles findbyAccountRole(String accountId) {
        // TODO Auto-generated method stub
        return myBatisDao.get("rolesSqlMapper.getById", accountId);
    }

    @Override
    public void addAccRole(RoleAccount roleAccount) {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.deleteAccountRole", (roleAccount.getAccountId().toString()));
        myBatisDao.save("rolesSqlMapper.addAccRole", roleAccount);

    }

    @Override
    public void addAccRole(String accountId, List<String> ids) {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.deleteAccountRole", accountId);
        for (String roleId : ids) {
            if (!Common.isEmpty(roleId)) {
                RoleAccount roleAccount = new RoleAccount();
                roleAccount.setAccountId(NumberUtils.parseLong(accountId));
                roleAccount.setRoleId(Integer.parseInt(roleId));
                myBatisDao.save("rolesSqlMapper.addAccRole", roleAccount);
            }
        }
    }

    public void deleteAccRole(String accountId) {
        myBatisDao.save("rolesSqlMapper.deleteAccountRole", accountId);
    }

    @Override
    public int countByParams(Map<String, Object> paramMap) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<Roles> findByParams(Map<String, Object> paramMap, Pager pager) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<Long> findAllResIdByRole(String roleId) {
        // TODO Auto-generated method stub
        return myBatisDao.getList("rolesSqlMapper.findAllResIdByRole", roleId);
    }

    @Override
    public List<Long> findAllResIdByRole(Long roleId) {
        // TODO Auto-generated method stub
        return myBatisDao.getList("rolesSqlMapper.findAllResIdByRoleId", roleId);
    }

    @Override
    public List<Roles> rolesList(Map<String, Object> paramMap, Pager pager) {
        // TODO Auto-generated method stub
        paramMap.put("pager", pager);
        return myBatisDao.getList("rolesSqlMapper.rolesList", paramMap);
    }

    @Override
    public Integer countRolesByParams(Map<String, Object> paramMap) {
        // TODO Auto-generated method stub
        return myBatisDao.get("rolesSqlMapper.countRoles", paramMap);
    }

    @Override
    public boolean updateRoles(Roles roles) {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.update", roles);
        return true;
    }

    @Override
    public boolean deleteRoles(Integer id) {
        // TODO Auto-generated method stub
        myBatisDao.delete("rolesSqlMapper.delete", id);
        return true;
    }

    @Override
    public boolean insertRoles(Roles roles) {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.add", roles);
        return true;
    }

    @Override
    public boolean updateStatus(List<Long> roleList, int status) {
        myBatisDao.save("rolesSqlMapper.updateRoleStatus", MapBuilder.create(HashMap.class).add("roleList", roleList).add("enable", status).map());
        return true;
    }

    @Override
    public List<Map<String, Object>> getResourceByRoleId(Long roleId) {
        // TODO Auto-generated method stub
        return myBatisDao.getList("rolesSqlMapper.getResourceByRoleId", roleId);
    }

    @Override
    public boolean deleteResByRoleId(Long roleId) {
        // TODO Auto-generated method stub
        myBatisDao.delete("rolesSqlMapper.deleteResByRoleId", roleId);
        return true;
    }

    @Override
    public boolean addResourcesBatch(Map<String, Object> paramMap) {
        // TODO Auto-generated method stub
        myBatisDao.save("rolesSqlMapper.addResourcesBatch", paramMap);
        return true;
    }

    @Override
    public Map<String, List<? extends ISelector>> provideData(String[] parentId) {
        // TODO Auto-generated method stub
        return null;
    }

    // @Override
    // public boolean updateStatus(Roles roles) {
    // // TODO Auto-generated method stub
    // myBatisDao.save("rolesSqlMapper.updateStatus", roles);
    // return true;
    // }
    // @Override
    // public boolean updateStatus(List<Long> accountIdList, int state) {
    // myBatisDao.save("accountSqlMapper.updateAccountStatus",
    // MapBuilder.create(HashMap.class)
    // .add("accountIdList", accountIdList)
    // .add("state", state).map());
    // return true;
    // }

}
